package myframe;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import ctrlzone.CtrlZoneManage;

/**
 * 主界面
 * 
 * @author season 2016/7/4
 * 
 */
public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;
	private JPanel contentPane; // 主面板
	private JMenuItem menuItem_deleteArea; // 删除控制区
	private CtrlZoneManage cManage; // 控制区管理
	private JMenuItem menuItem_addArea; // 添加控制区
	private JMenuItem menuItem_selectArea; // 选择控制区
	private JMenuItem menuItem_editArea; // 编辑控制区
	private JMenuItem menuItem_newArea; // 新建控制区
	private JMenuItem menuItem_cropManage; // 作物管理
	private JMenuItem menuItem_soilWater; // 土壤水基质势
	private JMenuItem menuItem_distillWater; // 蒸馏水电导率
	private JMenuItem menuItem_ganTu; // 干土介电常数
	private JMenuItem menuItem_irrigate; // 灌溉方式
	private JMenuItem menuItem_air; // 空气密度
	private JMenuItem menuItem_dryWetBall; // 干湿球常数
	private JMenuItem menuItem_timeManage; // 时间间隔管理
	private JMenuItem menuItem_quit; // 退 出
	private JMenuItem menuItem_cropArg; // 作物参数
	private JMenuItem menuItem_soilArg; // 土壤参数
	private JMenuItem menuItem_functionArg; // 功能参数
	private JMenuItem menuItem_baseData; // 基础信息
	private JMenuItem menuItem_weather; // 气象与预报
	private JCheckBoxMenuItem checkBoxMenuItem_PWDI; // PWDI
	private JCheckBoxMenuItem checkBoxMenuItem_perAreaWater; // 单位面积累积灌水量
	private JCheckBoxMenuItem checkBoxMenuItem_areaWater; // 控制区累积灌水量
	private JCheckBoxMenuItem checkBoxMenuItem_irrigate; // 灌溉控制过程
	private JCheckBoxMenuItem checkBoxMenuItem_cropRising; // 作物蒸腾速率
	private JCheckBoxMenuItem checkBoxMenuItem_perAreaRising; // 单位面积蒸腾量
	private JMenuItem menuItem_userHelp; // 帮助
	private JMenuItem menuItem_about; // 关于
	private JTabbedPane tab_PWDI; // PWDI选项卡
	private JTabbedPane tab_perAreaWater; // 单位面积累计灌水量选项卡
	private JTabbedPane tab_areaWater; // 控制区累积灌水量选项卡
	private JTabbedPane tab_irrigate; // 灌溉控制过程选项卡
	private JTabbedPane tab_cropRising; // 作物蒸腾速率选项卡
	private JTabbedPane tab_perAreaRising; // 单位面积蒸腾量选项卡

	private ArrayList<JCheckBoxMenuItem> cbMenuItems; // 显示参数的按钮，为了实现关闭选项卡时同步选中状态
	private ArrayList<JTabbedPane> tabs; // 选项卡数组，为了实现显示和隐藏选项卡
	private JTabbedPane tabbedPanes;
	private JMenu menu_manager;
	private JMenuItem menuItem_editManager; // 编辑管理员
	private JMenuItem menuItem_registerManager; // 注册管理员
	private JPopupMenu popupMenu; // 弹出菜单
	private JMenuItem menuItem_close; // 关闭当前选项卡
	private JMenuItem menuItem_close_other; // 关闭其他选项卡
	private JMenuItem menuItem_close_all; // 关闭所有选项卡

	private int popupIndexOfTab = -1; // 弹出菜单的选项卡的下标

	public MyFrame() {
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 733, 482);
		Image image = Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/img/icon.jpg"));
		setIconImage(image);
		setTitle("自动灌溉系统");

		cManage = new CtrlZoneManage();
		tabs = new ArrayList<>();
		cbMenuItems = new ArrayList<>();

		initView();
		initEvent();
	}

	/**
	 * 控件事件的处理
	 */
	private void initEvent() {
		menuItem_deleteArea.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cManage.delete();
			}
		});

		menuItem_addArea.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cManage.add();
			}
		});

		menuItem_editArea.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cManage.edit();
			}
		});

		ArgumentShowListener aListener = new ArgumentShowListener();
		checkBoxMenuItem_PWDI.addActionListener(aListener);
		checkBoxMenuItem_areaWater.addActionListener(aListener);
		checkBoxMenuItem_cropRising.addActionListener(aListener);
		checkBoxMenuItem_irrigate.addActionListener(aListener);
		checkBoxMenuItem_perAreaRising.addActionListener(aListener);
		checkBoxMenuItem_perAreaWater.addActionListener(aListener);

		tabbedPanes.addMouseListener(new TabMoveListener());

		CloseTabListener cTabListener = new CloseTabListener();
		menuItem_close.addActionListener(cTabListener);
		menuItem_close_other.addActionListener(cTabListener);
		menuItem_close_all.addActionListener(cTabListener);

	}

	/**
	 * 初始化界面
	 */
	private void initView() {

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu menu_system = new JMenu("\u7CFB\u7EDF");
		menuBar.add(menu_system);

		menuItem_newArea = new JMenuItem("\u65B0\u5EFA\u5DE5\u4F5C\u533A");
		menu_system.add(menuItem_newArea);

		JSeparator separator = new JSeparator();
		menu_system.add(separator);

		menuItem_cropManage = new JMenuItem(
				"\u4F5C\u7269\u53C2\u6570\u7BA1\u7406");
		menu_system.add(menuItem_cropManage);

		JMenu menu_soilManage = new JMenu(
				"\u571F\u58E4\u53C2\u6570\u7BA1\u7406");
		menu_system.add(menu_soilManage);

		menuItem_soilWater = new JMenuItem(
				"\u571F\u58E4\u6C34\u57FA\u8D28\u52BF");
		menu_soilManage.add(menuItem_soilWater);

		menuItem_distillWater = new JMenuItem(
				"\u84B8\u998F\u6C34\u7535\u5BFC\u7387");
		menu_soilManage.add(menuItem_distillWater);

		menuItem_ganTu = new JMenuItem("\u5E72\u571F\u4ECB\u7535\u5E38\u6570");
		menu_soilManage.add(menuItem_ganTu);

		JMenu menu_functionManage = new JMenu(
				"\u529F\u80FD\u53C2\u6570\u7BA1\u7406");
		menu_system.add(menu_functionManage);

		menuItem_irrigate = new JMenuItem("\u704C\u6E89\u65B9\u5F0F");
		menu_functionManage.add(menuItem_irrigate);

		menuItem_air = new JMenuItem("\u7A7A\u6C14\u5BC6\u5EA6");
		menu_functionManage.add(menuItem_air);

		menuItem_dryWetBall = new JMenuItem("\u5E72\u6E7F\u7403\u5E38\u6570");
		menu_functionManage.add(menuItem_dryWetBall);

		menuItem_timeManage = new JMenuItem(
				"\u65F6\u95F4\u95F4\u9694\u7BA1\u7406");
		menu_system.add(menuItem_timeManage);

		menuItem_quit = new JMenuItem("\u9000  \u51FA");
		menu_system.add(menuItem_quit);

		JMenu menu_controlArea = new JMenu("\u63A7\u5236\u533A");
		menuBar.add(menu_controlArea);

		menuItem_selectArea = new JMenuItem("\u9009\u62E9\u63A7\u5236\u533A");
		menu_controlArea.add(menuItem_selectArea);

		menuItem_addArea = new JMenuItem("\u6DFB\u52A0\u63A7\u5236\u533A");
		menu_controlArea.add(menuItem_addArea);

		menuItem_editArea = new JMenuItem("\u7F16\u8F91\u63A7\u5236\u533A");
		menu_controlArea.add(menuItem_editArea);

		menuItem_deleteArea = new JMenuItem("\u5220\u9664\u63A7\u5236\u533A");

		menu_controlArea.add(menuItem_deleteArea);

		JMenu menu_argInput = new JMenu("\u53C2\u6570\u8F93\u5165");
		menuBar.add(menu_argInput);

		menuItem_cropArg = new JMenuItem("\u4F5C\u7269\u53C2\u6570");
		menu_argInput.add(menuItem_cropArg);

		menuItem_soilArg = new JMenuItem("\u571F\u58E4\u53C2\u6570");
		menu_argInput.add(menuItem_soilArg);

		menuItem_functionArg = new JMenuItem("\u529F\u80FD\u53C2\u6570");
		menu_argInput.add(menuItem_functionArg);

		JMenu menu_dataGather = new JMenu("\u6570\u636E\u91C7\u96C6");
		menuBar.add(menu_dataGather);

		menuItem_baseData = new JMenuItem("\u57FA\u7840\u4FE1\u606F");
		menu_dataGather.add(menuItem_baseData);

		menuItem_weather = new JMenuItem("\u6C14\u8C61\u4E0E\u9884\u62A5");
		menu_dataGather.add(menuItem_weather);

		JMenu menu_center = new JMenu(
				"\u4E2D\u592E\u51B3\u7B56\u4E0E\u63A7\u5236");
		menuBar.add(menu_center);

		JMenu menu_showArg = new JMenu("\u663E\u793A\u53C2\u6570");
		menu_center.add(menu_showArg);

		checkBoxMenuItem_PWDI = new JCheckBoxMenuItem("PWDI");
		checkBoxMenuItem_PWDI.setName("0");
		checkBoxMenuItem_PWDI.setSelected(true);
		cbMenuItems.add(checkBoxMenuItem_PWDI);
		menu_showArg.add(checkBoxMenuItem_PWDI);

		checkBoxMenuItem_perAreaWater = new JCheckBoxMenuItem(
				"\u5355\u4F4D\u9762\u79EF\u7D2F\u79EF\u704C\u6C34\u91CF");
		checkBoxMenuItem_perAreaWater.setSelected(true);
		checkBoxMenuItem_perAreaWater.setName("1");
		cbMenuItems.add(checkBoxMenuItem_perAreaWater);
		menu_showArg.add(checkBoxMenuItem_perAreaWater);

		checkBoxMenuItem_areaWater = new JCheckBoxMenuItem(
				"\u63A7\u5236\u533A\u7D2F\u79EF\u704C\u6C34\u91CF");
		checkBoxMenuItem_areaWater.setSelected(true);
		checkBoxMenuItem_areaWater.setName("2");
		cbMenuItems.add(checkBoxMenuItem_areaWater);
		menu_showArg.add(checkBoxMenuItem_areaWater);

		checkBoxMenuItem_irrigate = new JCheckBoxMenuItem(
				"\u704C\u6E89\u63A7\u5236\u8FC7\u7A0B");
		checkBoxMenuItem_irrigate.setSelected(true);
		checkBoxMenuItem_irrigate.setName("3");
		cbMenuItems.add(checkBoxMenuItem_irrigate);
		menu_showArg.add(checkBoxMenuItem_irrigate);

		checkBoxMenuItem_cropRising = new JCheckBoxMenuItem(
				"\u4F5C\u7269\u84B8\u817E\u901F\u7387");
		checkBoxMenuItem_cropRising.setSelected(true);
		checkBoxMenuItem_cropRising.setName("4");
		cbMenuItems.add(checkBoxMenuItem_cropRising);
		menu_showArg.add(checkBoxMenuItem_cropRising);

		checkBoxMenuItem_perAreaRising = new JCheckBoxMenuItem(
				"\u5355\u4F4D\u9762\u79EF\u84B8\u817E\u91CF");
		checkBoxMenuItem_perAreaRising.setSelected(true);
		checkBoxMenuItem_perAreaRising.setName("5");
		cbMenuItems.add(checkBoxMenuItem_perAreaRising);
		menu_showArg.add(checkBoxMenuItem_perAreaRising);

		JMenuItem menuItem_setInfo = new JMenuItem(
				"\u8BBE\u7F6E\u63A7\u5236\u4FE1\u606F");
		menu_center.add(menuItem_setInfo);

		menu_manager = new JMenu("\u7BA1\u7406\u5458");
		menuBar.add(menu_manager);

		menuItem_editManager = new JMenuItem("\u7F16\u8F91\u7BA1\u7406\u5458");
		menu_manager.add(menuItem_editManager);

		menuItem_registerManager = new JMenuItem(
				"\u6CE8\u518C\u7BA1\u7406\u5458");
		menu_manager.add(menuItem_registerManager);

		JMenu menu_help = new JMenu("\u5E2E\u52A9");
		menuBar.add(menu_help);

		menuItem_userHelp = new JMenuItem("\u4F7F\u7528\u5E2E\u52A9");
		menu_help.add(menuItem_userHelp);

		menuItem_about = new JMenuItem("\u5173\u4E8E");
		menu_help.add(menuItem_about);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		contentPane.setLayout(new BorderLayout(0, 0));

		tabbedPanes = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPanes, BorderLayout.CENTER);

		tab_PWDI = new JTabbedPane(JTabbedPane.TOP);
		tab_PWDI.setName("  PWDI  ");
		tabbedPanes.addTab(tab_PWDI.getName(), null, tab_PWDI, null);
		tabs.add(tab_PWDI);

		tab_perAreaWater = new JTabbedPane(JTabbedPane.TOP);
		tab_perAreaWater.setName("单位面积累积灌水量");
		tabbedPanes.addTab(tab_perAreaWater.getName(), null, tab_perAreaWater,
				null);
		tabs.add(tab_perAreaWater);

		tab_areaWater = new JTabbedPane(JTabbedPane.TOP);
		tab_areaWater.setName("控制区累积灌水量");
		tabbedPanes.addTab(tab_areaWater.getName(), null, tab_areaWater, null);
		tabs.add(tab_areaWater);

		tab_irrigate = new JTabbedPane(JTabbedPane.TOP);
		tab_irrigate.setName("灌水控制过程");
		tabbedPanes.addTab(tab_irrigate.getName(), null, tab_irrigate, null);
		tabs.add(tab_irrigate);

		tab_cropRising = new JTabbedPane(JTabbedPane.TOP);
		tab_cropRising.setName("作物蒸腾速率");
		tabbedPanes
				.addTab(tab_cropRising.getName(), null, tab_cropRising, null);
		tabs.add(tab_cropRising);

		tab_perAreaRising = new JTabbedPane(JTabbedPane.TOP);
		tab_perAreaRising.setName("单位面积蒸腾量");
		tabbedPanes.addTab(tab_perAreaRising.getName(), null,
				tab_perAreaRising, null);
		tabs.add(tab_perAreaRising);

		// JTabbedPane tabbedPane_7 = new JTabbedPane(JTabbedPane.TOP);
		// tabbedPane.addTab("三维显示", null, tabbedPane_7, null);

		popupMenu = new JPopupMenu();
		addPopup(tabbedPanes, popupMenu);

		menuItem_close = new JMenuItem("\u5173\u95ED");
		menuItem_close.setActionCommand("self");
		popupMenu.add(menuItem_close);

		menuItem_close_other = new JMenuItem("\u5173\u95ED\u5176\u4ED6    ");
		menuItem_close_other.setActionCommand("other");
		popupMenu.add(menuItem_close_other);

		menuItem_close_all = new JMenuItem("\u5173\u95ED\u6240\u6709");
		menuItem_close_all.setActionCommand("all");
		popupMenu.add(menuItem_close_all);
	}

	/**
	 * 实现选项开添加或隐藏的监听类
	 * 
	 * @author season
	 * 
	 */
	class ArgumentShowListener implements ActionListener {

		private ArgumentShowListener() {
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			JCheckBoxMenuItem cMenuItem = ((JCheckBoxMenuItem) e.getSource());
			int index = Integer.parseInt(cMenuItem.getName());
			if (cMenuItem.isSelected()) {
				tabbedPanes.addTab(tabs.get(index).getName(), tabs.get(index));
				tabbedPanes.setSelectedIndex(tabbedPanes.getTabCount() - 1);
			} else {
				tabbedPanes.remove(tabs.get(index));
			}
		}
	}

	/**
	 * 实现选项卡移动的监听类
	 * 
	 * @author season
	 * 
	 */
	class TabMoveListener extends MouseAdapter {

		private JTabbedPane tabbedPane;
		private int srcIndex, desIndex;

		@Override
		public void mousePressed(MouseEvent e) {
			super.mousePressed(e);
			srcIndex = tabbedPanes.getSelectedIndex();
			tabbedPane = (JTabbedPane) tabbedPanes.getComponentAt(srcIndex);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			super.mouseReleased(e);
			desIndex = tabbedPanes.indexAtLocation(e.getX(), e.getY());
			if (null != tabbedPane && -1 != desIndex) {
				if (desIndex > srcIndex)
					tabbedPanes.insertTab(tabbedPane.getName(), null,
							tabbedPane, null, desIndex + 1);
				else {
					tabbedPanes.insertTab(tabbedPane.getName(), null,
							tabbedPane, null, desIndex);
				}
				tabbedPanes.setSelectedIndex(desIndex);
			}
			initArgs();
		}

		// 初始化参数
		private void initArgs() {
			tabbedPane = null;
			srcIndex = -1;
			desIndex = -1;
		}
	}

	/**
	 * 关闭选项卡的监听类
	 * 
	 * @author season
	 * 
	 */
	private class CloseTabListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			JTabbedPane tabbedPane = (JTabbedPane) tabbedPanes
					.getComponentAt(popupIndexOfTab);
			switch (e.getActionCommand()) {
			case "self":
				tabbedPanes.remove(tabbedPane);
				cbMenuItems.get(tabs.indexOf(tabbedPane)).setSelected(false);
				break;

			case "other":
				tabbedPanes.removeAll();
				tabbedPanes.addTab(tabbedPane.getName(), tabbedPane);
				for (int i = 0; i < cbMenuItems.size(); i++) {
					cbMenuItems.get(i).setSelected(false);
				}
				cbMenuItems.get(tabs.indexOf(tabbedPane)).setSelected(true);
				break;

			case "all":
				for (int i = 0; i < cbMenuItems.size(); i++) {
					cbMenuItems.get(i).setSelected(false);
				}
				tabbedPanes.removeAll();
				break;
			default:
				break;
			}
		}

	}

	/**
	 * 添加点击选选项卡右键事件
	 * 
	 * @param component
	 *            要添加事件的组件
	 * @param popup
	 *            菜单
	 */
	private void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {

				if (e.isPopupTrigger()) {
					 if (tabbedPanes.indexAtLocation(e.getX(), e.getY()) >= 0)
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					 if (tabbedPanes.indexAtLocation(e.getX(), e.getY()) >= 0)
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
				popupIndexOfTab = tabbedPanes.indexAtLocation(e.getX(),
						e.getY());
			}
		});
	}
}
